package logic

import (
	"context"
	"errors"
	"novel-world/model/mysql"

	"novel-world/novelrpc/internal/svc"
	"novel-world/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BooksDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBooksDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BooksDelLogic {
	return &BooksDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BooksDelLogic) BooksDel(in *novelrpc.BooksDelRequest) (*novelrpc.BooksDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Books
	begin := mysql.DB.Begin()
	if err := user.BooksFind(mysql.DB, in.BooksId); err != nil {
		begin.Rollback()
		return nil, errors.New("不存在该书架")
	}
	if err := user.BooksDel(mysql.DB, in.BooksId); err != nil {
		begin.Rollback()
		return nil, errors.New("删除失败")

	}
	mysql.DB.Commit()
	return &novelrpc.BooksDelResponse{}, nil
}
